package zhoufu.sz.sanxiao_check.presents;

import android.text.TextUtils;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.models.BaseModel;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.net.RequestRepository;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/8/8
 */
public class ModifyPassPresenter implements BasePresent
{

    private RequestRepository mRepository;

    private BasicMsgItem2Contract.View view;

    @Inject
    public ModifyPassPresenter(RequestRepository mRepository)
    {
        this.mRepository = mRepository;
    }

    public void bindView(BasicMsgItem2Contract.View view)
    {
        this.view = view;
    }

    @Override public void onStart()
    {

    }

    public void commit(String passOld, String pass1, String pass2)
    {
        if ( TextUtils.isEmpty(passOld) )
        {
            view.showToast(UIUtils.getString(R.string.input_old_password));
            return;
        }
        if ( TextUtils.isEmpty(pass1) )
        {
            view.showToast(UIUtils.getString(R.string.input_new_password));
            return;
        }
        if ( TextUtils.isEmpty(pass2) )
        {
            view.showToast(UIUtils.getString(R.string.reinput_new_password));
            return;
        }

        if ( passOld.length() < 6 || pass1.length() < 6 || pass2.length() < 6 )
        {
            view.showToast(UIUtils.getString(R.string.password_short));
            return;
        }
        if ( !pass1.equals(pass2) )
        {
            view.showToast(UIUtils.getString(R.string.password_difference));
            return;
        }

        mRepository.UpdatePassword(passOld, pass1, new RequestHelper.RequestCallBack<BaseModel>()
        {
            @Override public void onPullStart()
            {
                view.pullStart();
            }

            @Override public void onPullFailud(Throwable throwable)
            {
                view.pullFaild();
            }

            @Override public void onPullEnd()
            {
                view.pullEnd();
            }

            @Override public void onPullSuccessed(BaseModel s)
            {
                if ( s.isSuccess() )
                {
                    view.updateSuccese();
                } else
                {
                    view.showToast(s.getErrMsg());
                }
            }
        });
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        mRepository.onDestory();
    }
}
